home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.7 KB | 266 lines | [TEXT/CWIE] |
- package com.metrowerks.example.CWBlink;
-
- import java.awt.*;
-
- public class CWBlink extends java.applet.Applet
- {
- private Button blinkButton;
- private BlinkingText blinkText;
-
- public String getParameter(String name)
- {
- String result = null;
-
- try
- {
- result = super.getParameter(name);
- }
- catch ( Exception e )
- {
- result = null;
- }
-
- return result;
- }
-
- public void init()
- {
- Panel tempPanel;
-
- String att = getParameter("speed");
- int speed = (att == null) ?
- 500 : (1000 / Integer.valueOf(att).intValue());
-
- setLayout( new BorderLayout() );
-
- att = getParameter("blinker");
- String blinkString = (att == null) ?
- "CodeWarrior!!!" : att;
-
- blinkButton = new Button( "Blink" );
-
- tempPanel = new Panel();
- tempPanel.add( blinkButton );
- this.add( "North", tempPanel );
-
- blinkText = new BlinkingText( blinkString, speed );
-
- tempPanel = new Panel();
- tempPanel.add( blinkText );
- this.add( "Center", tempPanel );
-
- resize( 570, 170 );
- }
-
- public void start()
- {
- blinkText.start();
- }
-
- public void stop()
- {
- blinkText.stop();
- }
-
- public boolean action(Event evt, Object arg)
- {
- if ( "Blink".equals(arg) )
- {
- blinkText.ToggleBlinking();
- }
- return true;
- }
-
- public static void main(String args[])
- {
- com.metrowerks.AppletFrame.startApplet(
- "com.metrowerks.example.CWBlink.CWBlink",
- "Blink", args);
- }
- }
-
-
- class BlinkingText extends Canvas implements Runnable
- {
- private Thread blinkThread = null;
- private String blinkString;
- private Font font;
- private int speed;
- private boolean isBlinking = false;
- private Color[] letters;
- private boolean[] is_black;
- private int current_letter = 0;
- private int old_width, old_height;
-
- private Color RandomColor()
- {
- int red, green, blue;
-
- do {
- red = (int)(Math.random() * 256);
- } while ( red > 0x8000 );
-
- do {
- green = (int)(Math.random() * 256);
- } while ( green > 0x8000 );
-
- do {
- blue = (int)(Math.random() * 256);
- } while ( blue > 0x8000 );
-
- return new java.awt.Color( red, green, blue );
- }
-
- public BlinkingText( String blinkString, int speed )
- {
- this.blinkString = blinkString;
- this.speed = speed / blinkString.length();
- this.font = new java.awt.Font( "TimesRoman",
- Font.PLAIN, 64 );
- this.letters = new Color[blinkString.length()];
- this.is_black = new boolean[blinkString.length()];
-
- for ( int i = 0; i < letters.length; i++ )
- {
- letters[i] = RandomColor();
- is_black[i] = true;
- }
- resize( 530, 70 );
- repaint();
- }
-
- public synchronized void ToggleBlinking()
- {
- isBlinking = ! isBlinking;
- notify();
- }
-
- public synchronized void PaintLetter( int the_letter )
- {
- Rectangle b = bounds();
- int width = b.width;
- int height = b.height;
- Graphics g = getGraphics();
-
- if ( old_width == width && old_height == height )
- {
- int x = 0;
- int y = height;
-
- g.setFont(font);
- FontMetrics fm = g.getFontMetrics();
-
- for (int index=0; index<the_letter; index++ )
- {
- int w = fm.charWidth(blinkString.charAt(index));
- x += w;
- }
-
- int w = fm.charWidth(blinkString.charAt(the_letter));
- g.setColor( isBlinking ?
- letters[the_letter] : Color.black );
- g.clearRect( x, 0, w, height );
- g.drawString( blinkString.substring(
- the_letter,the_letter+1), x, y );
-
- is_black[the_letter] = !isBlinking;
- }
- else
- {
- paint( g );
- }
- }
-
- public synchronized void paint(Graphics g)
- {
- Rectangle b = bounds();
- int width = b.width;
- int height = b.height;
- old_width = width;
- old_height = height;
- int x = 0;
- int y = height;
-
- g.setColor(Color.black);
- g.setFont(font);
- FontMetrics fm = g.getFontMetrics();
-
- g.clearRect( 0, 0, width, height );
- if ( isBlinking )
- {
- for (int index=0; index<blinkString.length();
- index++ )
- {
- int w = fm.charWidth(blinkString.charAt(index));
-
- g.setColor( letters[index] );
-
- g.drawString(
- blinkString.substring(index,index+1), x, y);
- x += w;
- }
- }
- else
- {
- g.drawString(blinkString, x, y );
- }
-
- for ( int index=0; index<blinkString.length(); index++ )
- {
- is_black[index] = !isBlinking;
- }
- }
-
- public void start()
- {
- stop();
- blinkThread = new Thread(this);
- blinkThread.start();
- }
-
- public void stop()
- {
- if ( blinkThread != null )
- {
- blinkThread.stop();
- blinkThread = null;
- }
- }
-
- public void run()
- {
- while (true)
- {
- synchronized ( this )
- {
- try
- {
- wait( 1 );
- }
- catch (Exception e)
- {
- }
-
- if ( isBlinking || !is_black[current_letter] )
- {
- if ( isBlinking )
- {
- letters[current_letter] = RandomColor();
- }
- PaintLetter( current_letter );
- }
- current_letter++;
- if ( current_letter >= blinkString.length() )
- {
- current_letter = 0;
- }
- }
- }
- }
-
- public void finalize()
- {
- stop();
- }
- }
-